package com.unf.edu.rms.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unf.edu.rms.dao.Faculty;
import com.unf.edu.rms.dao.UserLogin;

@Service
public class FacultyService {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<Faculty> getFacultyList()
	{
		System.out.println("FacultyService.getFacultyList()==============");
		System.out.println("UserLoginService.validateUser()============="+sessionFactory.getCurrentSession());
		//System.out.println("UserLoginService.validateUser()====current Session===="+HibernateSessionFactory.getSessionFactory().getCurrentSession());
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Faculty";
		Query query = session.createQuery(hql);
		List <Faculty> facultyList = query.list();
		for(int i=0;i<facultyList.size();i++)
		{
			Faculty faculty = (Faculty)facultyList.get(i);
			if(faculty.getInactive().equals("N") || faculty.getInactive().equals("n")){
				faculty.setActive("Y");
			}
			else{
				faculty.setActive("N");
			}
			System.out.println("FacultyService.getFacultyList()============="+faculty.getFirstName());
			//System.out.println("FacultyService.getFacultyList()============="+faculty.getLastName());
			//System.out.println("FacultyService.getFacultyList()============="+faculty.getEmail());
		}
		return facultyList;
	}
	
	@Transactional
	public void addNewFaculty(Faculty faculty)
	{
		Session session = sessionFactory.getCurrentSession();
		session.save(faculty);
		
	}
	
	@Transactional
	public Faculty getFacultyById(int facultyId)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Faculty where facultyId=:facultyId";
		Query query = session.createQuery(hql);
		query.setInteger("facultyId", facultyId);
		Faculty faculty = (Faculty)query.list().get(0);
		return faculty;
	}
	
	
	@Transactional
	public void updateFaculty(Faculty faculty)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Faculty where facultyId=:facultyId";
		Query query = session.createQuery(hql);
		query.setInteger("facultyId", faculty.getFacultyId());
		session.update(faculty);
	}
}
